package com.example.server.controller;

import com.example.server.entity.Book;
import com.example.server.entity.Chapter;
import com.example.server.entity.Rating;
import com.example.server.service.BookService;
import com.example.server.service.ChapterService;
import com.example.server.service.RatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private RatingService ratingService;

    @GetMapping("/{id}")
    public Book getBookById(@PathVariable int id) {
        return bookService.getBookById(id);
    }

    @GetMapping
    public List<Book> getAllBooks() {
        return bookService.getAllBooks();
    }

    @GetMapping("/{id}/chapters")
    public List<Chapter> getChaptersByBookId(@PathVariable int id) {
        return chapterService.getChaptersByBookId(id);
    }

    @PostMapping
    public void insertBook(@RequestBody Book book) {
        bookService.insertBook(book);
    }

    @PutMapping("/{id}")
    public void updateBook(@PathVariable int id, @RequestBody Book book) {
        book.setIdbook(id);
        bookService.updateBook(book);
    }

    @DeleteMapping("/{id}")
    public void deleteBook(@PathVariable int id) {
        bookService.deleteBook(id);
    }

    @PostMapping("/{id}/click")
    public void incrementClicks(@PathVariable int id) {
        bookService.incrementClicks(id);
    }

    @PostMapping("/{id}/rating")
    public void submitRating(@PathVariable int id, @RequestBody Rating rating) {
        rating.setIdbook(id);
        ratingService.submitRating(rating);
    }

    @PostMapping("/batch")
    public List<Book> getBooksByIds(@RequestBody List<Integer> ids) {
        return bookService.getBooksByIds(ids);
    }
}
